<script setup>
import { ref } from 'vue'
// 控制抽屉的显示隐藏
const visibleDrawer = ref(false)
import ChannelSelect from './ChannelSelect.vue'
import { Plus } from '@element-plus/icons-vue'
//注册成局部组件
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'

import { artPublishService, artGetDetailService } from '@/api/article'
import { ElMessage } from 'element-plus'
// 默认数据
const defaultForm = ref({
  title: '', // 标题
  cate_id: '', // 分类对象
  cover_img: '', // 封面
  content: '', //string 内容
  state: '' //状态
})

// 准备数据
const formModel = ref({ ...defaultForm })

//组件对外暴露一个opne方法  基于open传来的参数，区分添加还是编辑
// open({})  =>表单无需渲染  说明是添加
// open({id,cate_name,....}) => 表单需要渲染，说明是编辑
// open 调用后  可以打开弹窗

const editorRef = ref()
const open = async (row) => {
  visibleDrawer.value = true
  if (row.id) {
    const res = await artGetDetailService(row.id)
    console.log(res)
  } else {
    formModel.value = { ...defaultForm }
    imgUrl.value = ''
    editorRef.value.setHTML('')
  }
}

defineExpose({
  open
})

// 准备数据 和 选择图片的处理逻辑
const imgUrl = ref('')
const onUploadFile = (uploadFile) => {
  console.log(uploadFile)

  imgUrl.value = URL.createObjectURL(uploadFile.raw)
  formModel.value.cover_img = uploadFile.raw
}

// 提交文章
const emit = defineEmits(['success'])
const onPublish = async (state) => {
  //将已发布的草稿状态，存入formModel
  formModel.value.state = state

  // 注意 当前接口 需要的是formData 对象
  // (将普通对象) => (转换成) => formData对象
  const fd = new FormData()
  for (let key in formModel.value) {
    fd.append(key, formModel.value[key])
  }

  // 发请求
  if (formModel.value.id) {
    //编辑操作 （回显）
    console.log('编辑操作')
  } else {
    // 添加操作
    await artPublishService(fd)
    ElMessage.success('添加成功')
    visibleDrawer.value = false

    //通知到父组件 添加成功了
    emit('success', 'add')
  }
}
</script>

<template>
  <!--  :title="formModel.id ? '编辑文章' : '添加文章'" -->
  <el-drawer
    v-model="visibleDrawer"
    title="编辑文章"
    direction="rtl"
    size="50%"
  >
    <!-- 发表文章表单 -->
    <el-form :model="formModel" ref="formRef" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <channel-select
          v-model="formModel.cate_id"
          width="100%"
        ></channel-select>
      </el-form-item>
      <el-form-item label="文章封面" prop="cover_img">
        <!-- 此处需要关闭element-plus 的自动上传，不需要配置action 等参数 
               只需要做前端的本地预览图片即可，无需在提前上传图标 
               语法 URL.createObjectURL(...)创建本地预览的地址 来预览
        -->
        <el-upload
          class="avatar-uploader"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="onUploadFile"
        >
          <img v-if="imgUrl" :src="imgUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <div class="editor">
          <quill-editor
            ref="editorRef"
            theme="snow"
            v-model:content="formModel.content"
            contentType="html"
          >
          </quill-editor>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="onPublish('已发布')" type="primary">发布</el-button>
        <el-button @click="onPublish('草稿')" type="info">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
